clear
current_directory = pwd;

num_comps = 16
%parpool('local',num_comps)

rng(17)

avg_literature_ag_shocks = importdata('avg_literature_ag_shocks.mat');
ag_productivity_shocks = avg_literature_ag_shocks';
manu_shocks = importdata('manu_productivity_shocks_2080_2099_CSIRO_adaptation_SSP5.mat')';
manu_shocks = ones(1,length(manu_shocks))+manu_shocks;
serv_shocks = importdata('serv_productivity_shocks_2080_2099_CSIRO_adaptation_SSP5.mat')';
serv_shocks = ones(1,length(serv_shocks))+serv_shocks;

% Choose which SSP growth scenario and update results output accordingly
% below
income_growth_2080 = importdata('income_growth_2080_SSP5.mat')';

baselinesimulation

ag_import_shares_mat_baseline = ag_import_share_mat;
ag_domestic_expenditures_vec_baseline = ag_import_share_mat(logical(eye(n_countries)));
manu_import_shares_mat_baseline = manu_import_share_mat;
manu_domestic_expenditures_vec_baseline = manu_import_share_mat(logical(eye(n_countries)));
ag_trade_mat_baseline = ag_trade_mat;
manu_trade_mat_baseline = manu_trade_mat;
ag_GDP_share_baseline = sim_ag_shares_GDP;
manu_GDP_share_baseline = sim_manu_shares_GDP;
serv_GDP_share_baseline = sim_serv_shares_GDP;
ag_exp_share_baseline = exp_share_ag_vec;
manu_exp_share_baseline = exp_share_manu_vec;
serv_exp_share_baseline = exp_share_serv_vec;
ag_net_exports_baseline = ag_GDP_share_baseline-ag_exp_share_baseline;
C_vec_baseline = C_vec;
P_vec_baseline = P_vec;
P_a_vec_baseline = P_a_vec;
P_m_vec_baseline = P_m_vec;
P_s_vec_baseline = P_s_vec;
C_a_vec_baseline = C_a_vec;
C_m_vec_baseline = C_m_vec;
C_s_vec_baseline = C_s_vec;
income_per_capita_baseline = wage_guesses;
total_income_vec_baseline = total_income_vec;
GDP_baseline = P_a_vec_baseline.*C_a_vec_baseline+P_m_vec_baseline.*C_m_vec_baseline+P_s_vec_baseline.*C_s_vec_baseline;
P_Tornqvist_vec_baseline = P_a_vec_baseline.^(ag_exp_share_baseline).*P_m_vec_baseline.^(manu_exp_share_baseline)...
    .*P_s_vec_baseline.^(serv_exp_share_baseline);
GDP_Tornqvist_baseline = total_income_vec_baseline./P_Tornqvist_vec_baseline;

lambda_vec = 1-((1-data_ag_labor_share).*data_ag_GDP_share)./(data_ag_labor_share.*(1-data_ag_GDP_share));
adj_cost_per_capita = lambda_vec.*income_per_capita_baseline;

T_multiplier_future = T_multiplier.*income_growth_2080;
ag_T_vec = data_ag_GDPpercap.*T_multiplier_future;
manu_T_vec = data_manu_GDPpercap.*T_multiplier_future;
serv_T_vec = data_serv_GDPpercap.*T_multiplier_future;

sim_Frechets
P_a_vec = geomean(ag_consumed_prices_mat);
P_m_vec = geomean(manu_consumed_prices_mat);
P_s_vec = geomean(serv_consumed_prices_mat);

find_equilibrium_parallel
sim_ag_shares_GDP = ag_income_vec./total_income_vec;
sim_manu_shares_GDP = manu_income_vec./total_income_vec;
sim_serv_shares_GDP = serv_income_vec./total_income_vec;

ag_import_shares_mat_baseline_future = ag_import_share_mat;
ag_domestic_expenditures_vec_baseline_future = ag_import_share_mat(logical(eye(n_countries)));
manu_import_shares_mat_baseline_future = manu_import_share_mat;
manu_domestic_expenditures_vec_baseline_future = manu_import_share_mat(logical(eye(n_countries)));
ag_trade_mat_baseline_future = ag_trade_mat;
manu_trade_mat_baseline_future = manu_trade_mat;
ag_GDP_share_baseline_future = sim_ag_shares_GDP;
manu_GDP_share_baseline_future = sim_manu_shares_GDP;
serv_GDP_share_baseline_future = sim_serv_shares_GDP;
ag_exp_share_baseline_future = exp_share_ag_vec;
manu_exp_share_baseline_future = exp_share_manu_vec;
serv_exp_share_baseline_future = exp_share_serv_vec;
ag_net_exports_baseline_future = ag_GDP_share_baseline_future-ag_exp_share_baseline_future;
C_vec_baseline_future = C_vec;
P_vec_baseline_future = P_vec;
P_a_vec_baseline_future = P_a_vec;
P_m_vec_baseline_future = P_m_vec;
P_s_vec_baseline_future = P_s_vec;
C_a_vec_baseline_future = C_a_vec;
C_m_vec_baseline_future = C_m_vec;
C_s_vec_baseline_future = C_s_vec;
income_per_capita_baseline_future = wage_guesses;
total_income_vec_baseline_future = total_income_vec;
GDP_baseline_future = P_a_vec_baseline_future.*C_a_vec_baseline_future+P_m_vec_baseline_future.*C_m_vec_baseline_future+P_s_vec_baseline_future.*C_s_vec_baseline_future;
P_Tornqvist_vec_baseline_future = P_a_vec_baseline_future.^(ag_exp_share_baseline_future).*P_m_vec_baseline_future.^(manu_exp_share_baseline_future)...
    .*P_s_vec_baseline_future.^(serv_exp_share_baseline_future);
GDP_Tornqvist_baseline_future = total_income_vec_baseline_future./P_Tornqvist_vec_baseline_future;

ag_T_vec = ag_T_vec.*ag_productivity_shocks;
manu_T_vec = manu_T_vec.*manu_shocks;
serv_T_vec = serv_T_vec.*serv_shocks;

sim_Frechets
P_a_vec = geomean(ag_consumed_prices_mat);
P_m_vec = geomean(manu_consumed_prices_mat);
P_s_vec = geomean(serv_consumed_prices_mat);

find_equilibrium_parallel
sim_ag_shares_GDP = ag_income_vec./total_income_vec;
sim_manu_shares_GDP = manu_income_vec./total_income_vec;
sim_serv_shares_GDP = serv_income_vec./total_income_vec;

ag_import_shares_mat_counterfactual_future = ag_import_share_mat;
ag_domestic_expenditures_vec_counterfactual_future = ag_import_share_mat(logical(eye(n_countries)));
manu_import_shares_mat_counterfactual_future = manu_import_share_mat;
manu_domestic_expenditures_vec_counterfactual_future = manu_import_share_mat(logical(eye(n_countries)));
ag_trade_mat_counterfactual_future = ag_trade_mat;
manu_trade_mat_counterfactual_future = manu_trade_mat;
ag_GDP_share_counterfactual_future = sim_ag_shares_GDP;
manu_GDP_share_counterfactual_future = sim_manu_shares_GDP;
serv_GDP_share_counterfactual_future = sim_serv_shares_GDP;
ag_exp_share_counterfactual_future = exp_share_ag_vec;
manu_exp_share_counterfactual_future = exp_share_manu_vec;
serv_exp_share_counterfactual_future = exp_share_serv_vec;
ag_net_exports_counterfactual_future = ag_GDP_share_counterfactual_future-ag_exp_share_counterfactual_future;
C_vec_counterfactual_future = C_vec;
P_vec_counterfactual_future = P_vec;
P_a_vec_counterfactual_future = P_a_vec;
P_m_vec_counterfactual_future = P_m_vec;
P_s_vec_counterfactual_future = P_s_vec;
C_a_vec_counterfactual_future = C_a_vec;
C_m_vec_counterfactual_future = C_m_vec;
C_s_vec_counterfactual_future = C_s_vec;
income_per_capita_counterfactual_future = wage_guesses;
total_income_vec_counterfactual_future = total_income_vec;
GDP_counterfactual_future = P_a_vec_counterfactual_future.*C_a_vec_counterfactual_future+P_m_vec_counterfactual_future.*C_m_vec_counterfactual_future+P_s_vec_counterfactual_future.*C_s_vec_counterfactual_future;
P_Tornqvist_vec_counterfactual_future = P_a_vec_counterfactual_future.^((ag_exp_share_baseline_future+ag_exp_share_counterfactual_future)/2)...
    .*P_m_vec_counterfactual_future.^((manu_exp_share_baseline_future+manu_exp_share_counterfactual_future)/2)...
    .*P_s_vec_counterfactual_future.^((serv_exp_share_baseline_future+serv_exp_share_counterfactual_future)/2);
GDP_Tornqvist_counterfactual_future = total_income_vec_counterfactual_future./P_Tornqvist_vec_counterfactual_future;


EV_future = calc_EV(C_vec_counterfactual_future, income_per_capita_baseline_future, P_a_vec_baseline_future, P_m_vec_baseline_future, P_s_vec_baseline_future, ...
    epsilon_a,epsilon_m,epsilon_s,sigma,omega_a,omega_m,omega_s,num_comps,n_countries);

GDP_Tornqvist_mat = [GDP_Tornqvist_baseline' GDP_Tornqvist_baseline_future' GDP_Tornqvist_counterfactual_future'];
GDP_Tornqvist_changes_mat = [GDP_Tornqvist_baseline_future'./GDP_Tornqvist_baseline' ...
    GDP_Tornqvist_counterfactual_future'./GDP_Tornqvist_baseline_future'];

ag_GDP_share_mat = [ag_GDP_share_baseline' ag_GDP_share_baseline_future' ag_GDP_share_counterfactual_future'];
manu_GDP_share_mat = [manu_GDP_share_baseline' manu_GDP_share_baseline_future' manu_GDP_share_counterfactual_future'];
serv_GDP_share_mat = [serv_GDP_share_baseline' serv_GDP_share_baseline_future' serv_GDP_share_counterfactual_future'];

ag_domestic_expenditures_mat = [ag_domestic_expenditures_vec_baseline ag_domestic_expenditures_vec_baseline_future ag_domestic_expenditures_vec_counterfactual_future];

food_prices_mat = [P_a_vec_baseline' P_a_vec_baseline_future' P_a_vec_counterfactual_future'];

Rawlsian_losses_log = Rawlsian_CE_log(L_vec,income_per_capita_baseline_future,EV_future);
Rawlsian_losses_CRRA = Rawlsian_CE_CRRA(L_vec,income_per_capita_baseline_future,EV_future);

outputs = [GDP_Tornqvist_changes_mat EV_future' ag_GDP_share_mat manu_GDP_share_mat serv_GDP_share_mat ag_domestic_expenditures_mat  ...
    ag_net_exports_baseline' ag_net_exports_baseline_future' ag_net_exports_counterfactual_future' food_prices_mat ...
    manu_shocks' serv_shocks' income_growth_2080' income_per_capita_baseline_future' Rawlsian_losses_log.*ones(n_countries,1) Rawlsian_losses_CRRA.*ones(n_countries,1)];

dlmwrite('results_future_2080_SSP5.csv',outputs)










